// import Vue from 'vue'
import axios from 'axios'
import router from '../router/index'

axios.defaults.withCredentials = true;
axios.withCredentials = true;
axios.crossDomain = true

const http = axios.create({
    // baseURL:'http://192.168.0.172/api/eve/',
    timeout: 1000 * 45,
    withCredentials: true
})
/**
 * 请求地址处理
 * @param {*} actionName action方法名称
 */
/*http.adornUrl = (actionName) => {
    // 非生产环境 && 开启代理, 接口前缀统一使用[/proxyApi/]前缀做代理拦截!
    return (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl) + actionName
}*/


// 请求拦截
http.interceptors.request.use(config=>{
    // console.log(config.data)
    deleteEmptyProp(config.data)
    config.headers['Token'] = window.sessionStorage.getItem('Token')
    return config
},error=>{
    return Promise.reject(error)
})
/**
 * 响应拦截
 */
http.interceptors.response.use(response => {
    var token = response.data.code
    if (token === 501) { // 501, 未登录
        window.sessionStorage.clear()
        router.push({ name: 'Login' })
    }

    return response
}, error => {
    console.log(error)
    return Promise.reject(error)
})

//阻止向后台发送为空的信息
function deleteEmptyProp(json) {
  if (typeof(json) === 'string') {
    return json
  } else {
    for (var item in json) {
      if (typeof json[item] === "object") {
        if (json[item] === null) {
          delete json[item];
        } else {
          deleteEmptyProp(json[item])
        }
      } else {
        if (typeof json[item] === 'string') {
          if (json[item].trim() === '') {
            delete json[item];
          }
        }
      }
    }
  }

}
export default http
